from Tkinter import *
from tkMessageBox import *

class MCalc(Frame):
    def __init__(self,master):
        Frame.__init__(self,master)
        self.pack()
        self.create_widgets()
    
    def create_widgets(self):
        self.result = Entry(self)
        self.result.grid(row = 0,column = 0,columnspan = 5,sticky = EW ,padx = 5,pady = 5)
        buttons = {}
        buttons['seven'] = Button(self,text = "7",width = 5,height=1,command= (lambda:self.add_number("7"))).grid(row = 1,column = 0,sticky = W,padx = 5,pady = 5)
        buttons['eight'] = Button(self,text = "8",width = 5,height=1,command= (lambda:self.add_number("8"))).grid(row = 1,column = 1,sticky = W,padx = 5,pady = 5)
        buttons['nine'] = Button(self,text = "9",width = 5,height=1,command= (lambda:self.add_number("9"))).grid(row = 1,column = 2,sticky = W,padx = 5,pady = 5)
        buttons['division'] = Button(self,text = "/",width = 5,height=1,command = (lambda:self.add_number("/"))).grid(row = 1,column = 3,sticky = W,padx = 5,pady = 5)
        buttons['about'] = Button(self,text = "About",width = 5,height=1,command = self.about).grid(row = 1,column = 4,sticky = NS,padx = 5,pady = 5)         
        buttons['four'] = Button(self,text = "4",width = 5,height=1,command = (lambda:self.add_number("4"))).grid(row = 2,column = 0,sticky = W,padx = 5,pady = 5)
        buttons['five'] = Button(self,text = "5",width = 5,height=1,command = (lambda:self.add_number("5"))).grid(row = 2,column = 1,sticky = W,padx = 5,pady = 5)
        buttons['six'] = Button(self,text = "6",width = 5,height=1,command = (lambda:self.add_number("6"))).grid(row = 2,column = 2,sticky = W,padx = 5,pady = 5)
        buttons['multiplication'] = Button(self,text = "*",width = 5,height=1,command = (lambda:self.add_number("*"))).grid(row = 2,column = 3,sticky = W,padx = 5,pady = 5)
        buttons['clear'] = Button(self,text = "c",width = 5,height=1,command = self.clear).grid(row = 2,column = 4,sticky = NS,padx = 5,pady = 5)         
        buttons['one'] = Button(self,text = "1",width = 5,height=1,command = (lambda:self.add_number("1"))).grid(row = 3,column = 0,sticky = W,padx = 5,pady = 5)
        buttons['two'] = Button(self,text = "2",width = 5,height=1,command = (lambda:self.add_number("2"))).grid(row = 3,column = 1,sticky = W,padx = 5,pady = 5)
        buttons['three'] = Button(self,text = "3",width = 5,height=1,command = (lambda:self.add_number("3"))).grid(row = 3,column = 2,sticky = W,padx = 5,pady = 5)
        buttons['subduction'] = Button(self,text = "-",width = 5,height=1,command = (lambda:self.add_number("-"))).grid(row = 3,column = 3,sticky = W,padx = 5,pady = 5)
        buttons['equal'] = Button(self,text = "=",width = 5,height=1,command = (self.equal)).grid(row = 3,column = 4,rowspan = 2,sticky = NS,padx = 5,pady = 5)
        buttons['zero'] = Button(self,text = "0",width = 5,height=1,command = (lambda:self.add_number("0"))).grid(row = 4,column = 0,columnspan = 2,sticky = EW,padx = 5,pady = 5)
        buttons['dot'] = Button(self,text = ".",width = 5,height=1,command = (lambda:self.add_number("."))).grid(row = 4,column = 2,sticky = W,padx = 5,pady = 5)
        buttons['addition'] = Button(self,text = "+",width = 5,height=1,command = (lambda:self.add_number("+"))).grid(row = 4,column = 3,sticky = W,padx = 5,pady = 5)                        
           
    def add_number(self,num):
        entry_txt = self.result.get()
        self.result.delete(0 , END)
        self.result.insert(0 , entry_txt + num)
    
    def operate(self,operator):
        pass

    def equal(self):
        entry_txt = self.result.get()
        self.result.delete(0 , END)
        self.result.insert(0 , repr(eval(entry_txt)))
    
    def clear(self):
        self.result.delete(0 , END)
    
    def about(self):
        showinfo("About", 'Version 1.0.0\nMCalc is a simple Open-Source Calculator written in Python and Tkinter.\nhttp://mcalc.googlecode.com')
    
root = Tk()
root.title("MCalc")
root.iconbitmap('mcalc.ico')
root.geometry("280x200")
root.resizable(False,False)
mcalc = MCalc(root)
root.mainloop()